﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebChart;


public partial class ADMonitor : System.Web.UI.Page
{
    protected int PageIndex = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

        //CreateChart("select [Date], [PV] FROM [GowllPV] where [CommunityID] = 30 and DATEDIFF(day,[date],getdate())<=7");
        

        if (Session["ADUser"] == null)
        {
            Response.Redirect("Advertisement.aspx");
        }

        if (!this.IsPostBack)
        {
            ViewState["Communitys_ID"] = 0;
            ViewState["Site_ID"] = 30;
            this.Bind("Select [ID],[title],[URL] from [GowllCommunitys] where [CreateStatue]=0");
            this.BindView("select top 1 [ID],[Title],[URL],[Price],[UserID],[DayPV] from [GowllCommunitys] where [ID]=0");
            this.CreateChart("SET LANGUAGE N'English' select ltrim(day([date]))+'-'+CONVERT(varchar(3),[date],107) as Date, [PV] FROM [GowllPV] where [CommunityID] = " + ViewState["Site_ID"] + " and DATEDIFF(day,[date],getdate())<=7 ORDER BY cast([date] as datetime) ASC");
            this.FlashView("select [Title],[Profile] from [GowllCommunitys] where [ID] = " + ViewState["Communitys_ID"]);

        }
    }
    void FlashView(string Sql) {
        //create a command object for the query


        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();
        SqlCommand myCommand = new SqlCommand(Sql, MySqlConnection);
        SqlDataReader reader = myCommand.ExecuteReader();

        FormView1.DataSource = reader;
        FormView1.DataBind();
        
        reader.Close();
        MySqlConnection.Close();
    }

    void CreateChart(string Sql) {
        //create a command object for the query
        SqlDataSource2.SelectCommand = Sql;
        axisx.DataSource = SqlDataSource2;
        axisy.DataSource = SqlDataSource2;
        axisx.DataBind();
        axisy.DataBind();

    }

    private void BindView(string Sql)
    {        
        this.SqlDataSource1.SelectCommand = Sql;
    }

    private void Bind(string Sql)
    {
        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        SqlDataAdapter MyDataAdapter = new SqlDataAdapter(Sql, MySqlConnection);
        DataSet ds = new DataSet();
        MyDataAdapter.Fill(ds);
        PagedDataSource PageData = new PagedDataSource();
        PageData.DataSource = ds.Tables[0].DefaultView;
        PageData.AllowPaging = true;
        PageData.PageSize = 24;
        if (this.PageIndex == -1)
        {
            this.PageIndex = PageData.PageCount - 1;
        }
        else if (this.PageIndex <= 0)
        {
            this.PageIndex = 0;
            this.LinkButton2.Visible = false;
        }
        else
        {
            this.LinkButton2.Visible = true;
        }
        if (this.PageIndex >= PageData.PageCount - 1)
        {
            this.LinkButton3.Visible = false;
            this.PageIndex = PageData.PageCount - 1;
        }
        else
        {
            this.LinkButton3.Visible = true;
        }
        PageData.CurrentPageIndex = this.PageIndex;
        DataList1.DataSource = PageData;
        DataList1.DataBind();
        this.Label3.Text = this.PageIndex.ToString();

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string s = ((LinkButton)sender).CommandArgument;
        s = s.Substring(s.Length - 10).Replace("/", "");
        int siteid=0;
        int.TryParse(s, out siteid);
        ViewState["Communitys_ID"] = ((LinkButton)sender).CommandName;
        ViewState["Site_ID"] = siteid;
        this.BindView("select top 1 [ID],[Title],[URL],[Price],[UserID],[DayPV] from [GowllCommunitys] where [ID]=" + ViewState["Communitys_ID"]);
        this.CreateChart("SET LANGUAGE N'English' select ltrim(day([date]))+'-'+CONVERT(varchar(3),[date],107) as Date, [PV] FROM [GowllPV] where [CommunityID] = " + siteid + " and DATEDIFF(day,[date],getdate())<=7 ORDER BY cast([date] as datetime) ASC");
        this.FlashView("select [Title],[Profile] from [GowllCommunitys] where [ID] = " + ViewState["Communitys_ID"]);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string key = this.TextBox1.Text;
        key = key.Replace("'", "").Trim();
        this.Bind("Select [ID],[title],[URL] from [GowllCommunitys] where [CreateStatue]=0 and [title] like '%" + key + "%'");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex--;
        this.Bind("Select [ID],[title],[URL] from [GowllCommunitys] where [CreateStatue]=0");
        this.CreateChart("SET LANGUAGE N'English' select ltrim(day([date]))+'-'+CONVERT(varchar(3),[date],107) as Date, [PV] FROM [GowllPV] where [CommunityID] = " + ViewState["Site_ID"] + " and DATEDIFF(day,[date],getdate())<=7 ORDER BY cast([date] as datetime) ASC");
        this.FlashView("select [Title],[Profile] from [GowllCommunitys] where [ID] = " + ViewState["Communitys_ID"]);

    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex++;
        this.Bind("Select [ID],[title],[URL] from [GowllCommunitys] where [CreateStatue]=0");
        this.CreateChart("SET LANGUAGE N'English' select ltrim(day([date]))+'-'+CONVERT(varchar(3),[date],107) as Date, [PV] FROM [GowllPV] where [CommunityID] = " + ViewState["Site_ID"] + " and DATEDIFF(day,[date],getdate())<=7 ORDER BY cast([date] as datetime) ASC");
        this.FlashView("select [Title],[Profile] from [GowllCommunitys] where [ID] = " + ViewState["Communitys_ID"]);

    }

}
